package com.springboot.config.security.admin;

import com.springboot.entity.Administrator;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @descrition:
 * @author: lzh
 */
@Data
public class AdministratorDetails implements UserDetails {

    private Integer id;

    private String username;

    private String password;

    private Collection<? extends GrantedAuthority> authorities;

    public AdministratorDetails(){
        super();
    }

    public AdministratorDetails(Administrator administrator) {
        this.id = administrator.getId();
        this.username = administrator.getUsername();
        this.password = administrator.getPassword();
        Set<GrantedAuthority> authoritiesSet = new HashSet();
        authoritiesSet.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        if(administrator.getIsSuper()) {
            //添加超级管理员权限
            authoritiesSet.add(new SimpleGrantedAuthority("ROLE_SUPER"));
        }
        authorities = authoritiesSet;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
